package cn.com.lyj6851.modules.template.service.impl;

import cn.com.lyj6851.common.service.impl.BaseServiceImpl;
import cn.com.lyj6851.common.utils.QueryHelpPlus;
import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.modules.template.domain.YdyShippingTemplatesRegion;
import cn.com.lyj6851.modules.template.service.YdyShippingTemplatesRegionService;
import cn.com.lyj6851.modules.template.service.dto.YdyShippingTemplatesRegionDto;
import cn.com.lyj6851.modules.template.service.dto.YdyShippingTemplatesRegionQueryCriteria;
import cn.com.lyj6851.modules.template.service.mapper.YdyShippingTemplatesRegionMapper;
import cn.com.lyj6851.utils.FileUtil;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxShippingTemplatesRegion")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YdyShippingTemplatesRegionServiceImpl extends BaseServiceImpl<YdyShippingTemplatesRegionMapper, YdyShippingTemplatesRegion> implements YdyShippingTemplatesRegionService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(YdyShippingTemplatesRegionQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YdyShippingTemplatesRegion> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YdyShippingTemplatesRegionDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<YdyShippingTemplatesRegion> queryAll(YdyShippingTemplatesRegionQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YdyShippingTemplatesRegion.class, criteria));
    }


    @Override
    public void download(List<YdyShippingTemplatesRegionDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YdyShippingTemplatesRegionDto yxShippingTemplatesRegion : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("省ID", yxShippingTemplatesRegion.getProvinceId());
            map.put("模板ID", yxShippingTemplatesRegion.getTempId());
            map.put("城市ID", yxShippingTemplatesRegion.getCityId());
            map.put("首件", yxShippingTemplatesRegion.getFirst());
            map.put("首件运费", yxShippingTemplatesRegion.getFirstPrice());
            map.put("续件", yxShippingTemplatesRegion.getContinues());
            map.put("续件运费", yxShippingTemplatesRegion.getContinuePrice());
            map.put("计费方式", yxShippingTemplatesRegion.getType());
            map.put("分组唯一值", yxShippingTemplatesRegion.getUniqid());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
